﻿using Ecommerce.Domain;
using Ecommerce.Repository.Impl;
using Ecommerce.Repository.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using Ecommerce.API.Application.Command.RoleCommand;
using System.Security.Principal;
using Ecommerce.Domain.User;
using Ecommerce.Services.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using System.Security.Claims;
using Ecommerce.Domain.User;

namespace Ecommerce.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class InfoController : ControllerBase
    {
        private readonly IUseInfoServices services;

        private readonly IMediator mediator;
        private readonly IConfiguration configuration;

        public InfoController(IUseInfoServices services, IMediator mediator, IConfiguration configuration)
        {
            this.services = services;
            this.mediator = mediator;
            this.configuration = configuration;
        }

        private string JwtNewString(string Uid )
        {
            var signingAlogorithm = SecurityAlgorithms.HmacSha256;
            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub,Uid),
                new Claim(ClaimTypes.Role,"asahi"),
                new Claim(ClaimTypes.UserData,"测试")
            };
            //私钥
            var secretByte = Encoding.UTF8.GetBytes(configuration["Authentication:SecretKey"]);
            // 非对称加密
            var signingKey = new SymmetricSecurityKey(secretByte);
            // 使用256 生成数字签名
            var signingCredentials = new SigningCredentials(signingKey, signingAlogorithm);
            // 生成Token
            var token = new JwtSecurityToken(
                issuer: configuration["Authentication:Issuer"],
                audience: configuration["Authentication:Audience"],
                claims: claims,
                notBefore:DateTime.UtcNow,            //发布日期
                expires: DateTime.UtcNow.AddDays(1), // 一天后过期
                signingCredentials
            );
            // 生成token 字符串
            var strToken = new JwtSecurityTokenHandler().WriteToken(token);
            return strToken;
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<string> GetLogin(string Name, string UserPaw)
        {
            var list = await services.GetLogin(Name, UserPaw);
            JwtNewString(list.FirstOrDefault().Uid.ToString());
            if (JwtNewString(list.FirstOrDefault().Uid.ToString())==null)
            {
                return "-1";
            }
            else
            {
                return      JwtNewString(list.FirstOrDefault().Uid.ToString());
            }
           
        }
        [HttpPost]
        public async Task<List<Role>> GetRoles(RoleGetCommand role)
        {
            return await mediator.Send(role);
        }
        [HttpPost]
        public async Task<object> GetUser(UserGetCommand user)
        {
            return await mediator.Send(user);
        }                       
        [HttpPost]
        public async Task<object> GetAccount(AccountGetCommand account)
        {
            return await mediator.Send(account);
        }
        [HttpPost]
        public async Task<List<Account>> GetNameUser(AccountIdCommand accountId)
        {
            return await mediator.Send(accountId);
        }
        [HttpPost]
        public async Task<int> AddAccount(AccountAddCommand account)
        {
            return await mediator.Send(account);
        }

        [HttpPost]
        public async Task<List<Account>> AccountLish(AccountNameCommand account)
        {
            return await mediator.Send(account);
        }
        [HttpPost]
        public async Task<int> UpdateAccount(AccountUpdateCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
